package com.level2app.view.calendar;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.level2app.R;
import com.level2app.utils.ScreenUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class CalendarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    public ArrayList<DateBean> data;
    private OnRecyclerviewItemClick onRecyclerviewItemClick;

    public OnRecyclerviewItemClick getOnRecyclerviewItemClick() {
        return onRecyclerviewItemClick;
    }

    public void setOnRecyclerviewItemClick(OnRecyclerviewItemClick onRecyclerviewItemClick) {
        this.onRecyclerviewItemClick = onRecyclerviewItemClick;
    }

    public CalendarAdapter(Context context, ArrayList<DateBean> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == DateBean.itemTypeDay) {
            final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_day, parent, false);
            final DayViewHolder dayViewHolder = new DayViewHolder(view);
            dayViewHolder.itemView.setOnClickListener(v -> {
                if (onRecyclerviewItemClick != null) {
                    onRecyclerviewItemClick.onItemClick(v, dayViewHolder.getLayoutPosition());
                }
            });
            return dayViewHolder;
        } else if (viewType == DateBean.itemTypeMonth) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_month, parent, false);
            final MonthViewHolder monthViewHolder = new MonthViewHolder(view);
            monthViewHolder.itemView.setOnClickListener(v -> {
                if (onRecyclerviewItemClick != null) {
                    onRecyclerviewItemClick.onItemClick(v, monthViewHolder.getLayoutPosition());
                }
            });
            return monthViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MonthViewHolder) {
            MonthViewHolder viewHolder = (MonthViewHolder) holder;
            viewHolder.month.setText(data.get(position).getMonthStr());
        } else {
            DayViewHolder viewHolder = (DayViewHolder) holder;

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date todayDate = new Date();
            String todayStr = format.format(todayDate);//获取今天日期
            viewHolder.dayTv.setText(data.get(position).getDay());
            //非空数据
            if (!TextUtils.isEmpty(data.get(position).getDay())) {
                //是否是今天以前
                Date beforeToday = data.get(position).getDate();
                String currentDateStr = data.get(position).getMonth()+"-"+data.get(position).getDay();//获取得到的日期
                if (currentDateStr.equals(todayStr)) {
                    //今天
                    viewHolder.dayTv.setTextColor(Color.BLACK);
                } else if (beforeToday.getTime() < todayDate.getTime()) {
                    //今天之前的日期  设置成灰色
                    viewHolder.dayTv.setTextColor(Color.parseColor("#A7AEC1"));
                    Log.e("====", "before=" + beforeToday.toString() + ", today =" + todayDate.toString());
                } else {
                    viewHolder.dayTv.setTextColor(Color.BLACK);
                }
            } else {
                viewHolder.dayTv.setTextColor(Color.BLACK);
            }

            DateBean dateBean = data.get(position);
            //设置item状态
            if (dateBean.getItemState() == DateBean.ITEM_STATE_BEGIN_DATE || dateBean.getItemState() == DateBean.ITEM_STATE_END_DATE) {
                viewHolder.dayLl.setBackgroundResource(R.drawable.shape_243949_to_517fa4_8dp);
                viewHolder.dayTv.setTextColor(Color.WHITE);
//                viewHolder.tv_check_in_check_out.setVisibility(VISIBLE);
//                if (dateBean.getItemState() == DateBean.ITEM_STATE_BEGIN_DATE) {
//                    viewHolder.tv_check_in_check_out.setText("开始");
//                } else {
//                    viewHolder.tv_check_in_check_out.setText("结束");
//                }
            } else if (dateBean.getItemState() == DateBean.ITEM_STATE_SELECTED) {
                //选中状态
//                viewHolder.dayLl.setBackgroundResource(R.drawable.shape_accbee_to_e7f0fd_8dp);
                viewHolder.dayLl.setBackgroundResource(R.drawable.shape_e7f0fd_8dp);
                viewHolder.dayTv.setTextColor(Color.BLACK);
            } else {
                //正常状态
                viewHolder.dayLl.setBackgroundColor(Color.WHITE);
//                viewHolder.tv_check_in_check_out.setVisibility(GONE);
            }
            //背景色设置
            viewHolder.itemView.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
            viewHolder.bgView.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
            RelativeLayout.LayoutParams layoutParamsNormal = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            layoutParamsNormal.setMargins(0, 0, 0, 0);
            viewHolder.bgView.setLayoutParams(layoutParamsNormal);
            if (dateBean.getSignState() == DateBean.ITEM_START_LINE) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                layoutParams.setMargins(ScreenUtil.dip2px(20), 0, 0, 0);
                viewHolder.bgView.setLayoutParams(layoutParams);
                viewHolder.bgView.setBackgroundResource(R.drawable.shape_e7f0fd_left_8dp);
            } else if (dateBean.getSignState() == DateBean.ITEM_END_LINE) {
                RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                layoutParams.setMargins(0, 0, ScreenUtil.dip2px(20), 0);
                viewHolder.bgView.setLayoutParams(layoutParams);
                viewHolder.bgView.setBackgroundResource(R.drawable.shape_e7f0fd_right_8dp);
            } else if (dateBean.getSignState() == DateBean.ITEM_MIDDLE_LINE) {
                viewHolder.bgView.setBackgroundColor(ContextCompat.getColor(context, R.color.color_e7f0fd));
            } else {
                viewHolder.bgView.setBackgroundColor(ContextCompat.getColor(context, R.color.white));
            }
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getItemType();
    }

    public static class DayViewHolder extends RecyclerView.ViewHolder {
        public TextView dayTv;
        public LinearLayout dayLl;
        public View bgView;
//        public TextView tv_check_in_check_out;

        public DayViewHolder(@NonNull View itemView) {
            super(itemView);
            dayTv = itemView.findViewById(R.id.dayTv);
            dayLl = itemView.findViewById(R.id.dayLl);
            bgView = itemView.findViewById(R.id.bgView);
//            tv_check_in_check_out = itemView.findViewById(R.id.tv_check_in_check_out);
        }
    }

    public static class MonthViewHolder extends RecyclerView.ViewHolder {
        public TextView month;

        public MonthViewHolder(@NonNull View itemView) {
            super(itemView);
            month = itemView.findViewById(R.id.tv_month);
        }
    }

    public interface OnRecyclerviewItemClick {
        void onItemClick(View v, int position);
    }

    public void reset(){

    }
}